package vn.sunnet.backviet01.game.coloa.human.enemy;

import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import vn.sunnet.backviet01.game.coloa.constant.Constants;

public class Aurra extends Enemy {
	private TiledTextureRegion mAurraDieTiledTextureRegion;
	private long durationDie;
	private long durationMove;

	private float xMove;
	private float yMove;

	private boolean isDie = false;

	private long countDelay = 0;

	public Aurra(int tiledAttack, float pX, float pY,
			TiledTextureRegion pTiledTextureRegion) {
		super(tiledAttack, pX, pY, pTiledTextureRegion);
		this.name = Constants.AURRA;
	}

	public void onManagedUpdate(float pSecondsElapsed) {

		if (isAllowChangeStatus) {
			if (status == Constants.DIE) {
				if (getTextureRegion() != mTextureRegionMove)
					updateTextureRegion(mTextureRegionMove, true);
				die();
			} else if (isFreezed && countDelay == 0) {
				this.stopAnimation();
				updateTextureRegion(mTextureRegion_next);
				countDelay = System.currentTimeMillis();
				unregisterUpdateHandler(handler);
			} else if (isStun && startStun == 0) {
				startStun = System.currentTimeMillis();
				setStunAction();
			} else if (status == Constants.ATTACK) {
				unregisterUpdateHandler(handler);
			}
			isAllowChangeStatus = false;
		} else if (isFreezed && this.hitPoints > 0) {
			if (countDelay + timeFreezed < System.currentTimeMillis()) {
				move();
				if (status == Constants.MOVE) {
					registerUpdateHandler(handler);
				} else if (status == Constants.ATTACK) {
					unregisterUpdateHandler(handler);
				}
				isFreezed = false;
				countDelay = 0;
			}
		} else if (isStun && this.hitPoints > 0) {
			if (timeStun + startStun < System.currentTimeMillis()) {
				resetStunAction();
				isStun = false;
				startStun = 0;
			}
		}

		if (status == Constants.ATTACK
				&& this.getCurrentTileIndex() != tiledAttack)
			isAttack = false;

		else if (status == Constants.DIE && this.life) {
			setAlpha(getAlpha() - 0.02f);
		}

		xContain = this.getX();
		yContain = this.getY();

		super.onManagedUpdate(pSecondsElapsed);
	}

	public void setDieTextureRegion(
			TiledTextureRegion pAurraDieTiledTextureRegion) {
		this.mAurraDieTiledTextureRegion = pAurraDieTiledTextureRegion;
	}

	public void setDurationDie(long durationDie) {
		this.durationDie = durationDie;
	}

	public boolean isDie() {
		return this.isDie;
	}

	private void die() {
		this.stopAnimation();
		this.updateTextureRegion(mAurraDieTiledTextureRegion, true);
		this.animate(durationDie);
		this.setPosition(this.getX(), this.getY() + 80);
	}

	public void positionMove(float xP, float yP) {
		this.xMove = xP;
		this.yMove = yP;
		this.handler.setVelocity(xMove, yMove);
	}

	public void setDurationMove(long durationMove) {
		this.durationMove = durationMove;
	}

	public void move() {
		if (getTextureRegion() != mTextureRegionMove)
			updateTextureRegion(mTextureRegionMove.deepCopy(), true);
		this.animate(durationMove);
	}

	public void updateTextureRegion(TiledTextureRegion pTextureRegion,
			boolean isReset) {
		super.updateTextureRegion(pTextureRegion, isReset);
		widthContain = pTextureRegion.getTileWidth() - 20;
		heightContain = pTextureRegion.getTileHeight() - 20;
	}

	protected void createContain() {
		xContain = this.getX() + 10;
		yContain = this.getY() + 10;

		widthContain = this.getWidth() - 20;
		heightContain = this.getHeight() - 20;
	}

	public void setStunAction() {
		int index = this.getCurrentTileIndex();
		animate(durationMove * 2);
		handler.setVelocityX(handler.getVelocityX() / 2);
		setCurrentTileIndex(index);
	}

	public void resetStunAction() {
		int index = this.getCurrentTileIndex();
		animate(durationMove);
		handler.setVelocityX(handler.getVelocityX() * 2);
		setCurrentTileIndex(index);
		
	}
}
